package org.dromara.business.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.business.contracts.StakingVaultNative;
import org.springframework.stereotype.Service;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.utils.Convert;

import java.math.BigDecimal;
import java.math.BigInteger;

@Slf4j
@RequiredArgsConstructor
@Service
public class StakingContractsService {

    private final StakingVaultNative stakingVaultNative;

    /**
     *
     * @param productId 商品Id
     * @param lockDuration 锁定时间，单位秒
     * @param dailyRateBP 日收益 单位0.01% = 1
     * @param interestClaimable 定期间是否允许中途领息
     * @param active 是否有效
     * @return
     * @throws Exception
     */
    public String editProduct(BigInteger productId,
                             BigInteger lockDuration, BigInteger dailyRateBP, BigDecimal amount, Boolean interestClaimable,
                             Boolean active) throws Exception {
        BigInteger minDeposit = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();
        TransactionReceipt receipt = stakingVaultNative.setProduct(productId, lockDuration, dailyRateBP, minDeposit, interestClaimable, active).send();
        return receipt.getTransactionHash();
    }

    /**
     * 向合约地址转入收益金额待发放
     * @param amount
     * @return
     * @throws Exception
     */
    public String fundReward(BigInteger amount)throws Exception{
        TransactionReceipt receipt = stakingVaultNative.fundReward(amount).send();
        return receipt.getTransactionHash();
    }
}
